﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fcdbas.Biz;
using Fcdbas.Core;

namespace Fcdbas.WinUI.Common
{
    public sealed class ApprovalRouting
    {
        public static List<approval_routing> DisplayApprovalRouting(string key_name,string key_ref)
        {
            //if (AppEnvironment.Session.IsRoot)
            //    return true;
            if (!Fcdbas.WinUI.Common.Security.IsViewAvaliable(Fcdbas.WinUI.Common.AppEnvironment.Session.USER_ID, "approval_routing"))
            {
                Fcdbas.WinUI.Common.WinTranslate.GetTranslate().ShowMessage("没有查询号视图[%%1]的权限，请联系系统管理员。", "approval_routing");
                return null;
            }
            string where = Fcdbas.Biz.FndDAL.BuildCondition(new WhereValue("key_name", key_name, "=", false),new WhereValue("key_ref",key_ref,"=",false));
            
            

          //  List<fnd_user_profile> list = fnd_user_profile.FindAll(where, "", "", 0, 0);
            List<approval_routing> approvalRouting = approval_routing.FindAll(where, "step_no", "PersonInfo_Get(person_id,'person_name') as Reference1,PersonGroup_Get(group_id,'group_description') as Reference2", 0, 0);
            return approvalRouting;
        }

        public static List<approval_profile> DisplayApprovalProfile()
        {
            if (!Fcdbas.WinUI.Common.Security.IsViewAvaliable(Fcdbas.WinUI.Common.AppEnvironment.Session.USER_ID, "approval_profile"))
            {
                Fcdbas.WinUI.Common.WinTranslate.GetTranslate().ShowMessage("没有查询号视图[%%1]的权限，请联系系统管理员。", "approval_profile");
                return null;
            }
            List<approval_profile> approvalProfile = Fcdbas.Biz.approval_profile.FindAll();
            return approvalProfile;
        }
        public static bool ApprovalRouting_CreateRouting(string keyName, string keyRef, string template_id_)
        {
            SalString sRemove = "ApprovalRouting_CreateRouting(:key_name_,:key_ref_,:template_id_)";
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("key_name_", keyName);
            dic.Add("key_ref_", keyRef);
            dic.Add("template_id_", template_id_);
            SalNumber res = cSessionManager.DbMySQLBlock(sRemove, dic, System.Data.CommandType.StoredProcedure);
            if (res > 0)
                cSessionManager.DbTransactionEnd();
            else
            {
                cSessionManager.DbTransactionClear();
                SalString msg = "";
                cSessionManager.GetSqlLastError(ref msg);
                WinTranslate.GetTranslate().ShowMessage(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return res;
        }
        public static bool ApprovalRouting_RemoveStep(string keyName,string keyRef,decimal? stopNo)
        {
            SalString sRemove = "ApprovalRouting_RemoveStep(:key_name_,:key_ref_,:step_no_)";
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("key_name_", keyName);
            dic.Add("key_ref_", keyRef);
            dic.Add("step_no_", stopNo);
            SalNumber  res = cSessionManager.DbMySQLBlock(sRemove, dic,System.Data.CommandType.StoredProcedure);
            if (res > 0)
                cSessionManager.DbTransactionEnd();
            else
            {
                cSessionManager.DbTransactionClear();
                SalString msg = "";
                cSessionManager.GetSqlLastError(ref msg);
                WinTranslate.GetTranslate().ShowMessage(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return res;
        }
        public static bool ApprovalRouting_Modify(approval_routing ar, string attr, string action)
        {
            SalString sRemove = "ApprovalRouting_Modify__(:info_,:objid_,:objversion_,:attr_,:action_)";
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("info_", "");
            dic.Add("objid_", ar.objid);
            dic.Add("objversion_", ar.objversion);
            dic.Add("attr_", attr);
            dic.Add("action_", action);
            SalNumber res = cSessionManager.DbMySQLBlock(sRemove, dic, System.Data.CommandType.StoredProcedure);
            if (res > 0)
                cSessionManager.DbTransactionEnd();
            else
            {
                cSessionManager.DbTransactionClear();
                SalString msg = "";
                cSessionManager.GetSqlLastError(ref msg);
                WinTranslate.GetTranslate().ShowMessage(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return res;
        }
        public static bool ApprovalRouting_IsAllowReject(string key_name_, string key_ref_, decimal? step_no_)
        {
            SalString sSql = "SELECT ApprovalRouting_IsAllowReject('" + key_name_ + "','" + key_ref_ + "','" + step_no_.ToString() + "' );";
            return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
        }
        public static bool ApprovalRouting_IsLuAllow(string key_name_)
        {
            SalString sSql = "SELECT ApprovalRouting_IsLuAllow('" + key_name_ +  "' );";
            return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
        }

        public static bool ApprovalRouting_IsAllowApproval(string key_name_, string key_ref_, decimal? step_no_)
        {
            SalString sSql = "SELECT ApprovalRouting_IsAllowApproval('" + key_name_ + "','" + key_ref_ + "','" + step_no_.ToString() + "' );";
            return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
        }
        public static string ApprovalRouting_GetKeyDesc(string key_name_, string key_ref_)
        {
            SalString sSql = "SELECT ApprovalRouting_GetKeyDesc('" + key_name_ + "','" + key_ref_ + "' );";
            return cSessionManager.GetUserFunctionValue<string>(sSql, false);
        }
        public static bool ApprovalRouting_IsAllowRevise(string key_name_, string key_ref_, decimal? step_no_)
        {
            SalString sSql = "SELECT ApprovalRouting_IsAllowRevise('" + key_name_ + "','" + key_ref_ + "','" + step_no_.ToString() + "' );";
            return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
        }

        public static bool ApprovalRouting_DoApproval(string key_name_, string key_ref_, decimal? step_no_, string approval_note_)
        {
            SalString sSql = "ApprovalRouting_DoApproval(:key_name_,:key_ref_,:step_no_,:approval_note_)";
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("key_name_", key_name_);
            dic.Add("key_ref_", key_ref_);
            dic.Add("step_no_", step_no_);
            dic.Add("approval_note_", approval_note_);
            SalNumber res = cSessionManager.DbMySQLBlock(sSql, dic, System.Data.CommandType.StoredProcedure);
            if (res > 0)
                cSessionManager.DbTransactionEnd();
            else
            {
                cSessionManager.DbTransactionClear();
                SalString msg = "";
                cSessionManager.GetSqlLastError(ref msg);
                WinTranslate.GetTranslate().ShowMessage(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return res;
        }

        public static bool ApprovalRouting_DoReject(string key_name_, string key_ref_, decimal? step_no_, string approval_note_)
        {
            SalString sSql = "ApprovalRouting_DoReject(:key_name_,:key_ref_,:step_no_,:approval_note_)";
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("key_name_", key_name_);
            dic.Add("key_ref_", key_ref_);
            dic.Add("step_no_", step_no_);
            dic.Add("approval_note_", approval_note_);
            SalNumber res = cSessionManager.DbMySQLBlock(sSql, dic, System.Data.CommandType.StoredProcedure);
            if (res > 0)
                cSessionManager.DbTransactionEnd();
            else
            {
                cSessionManager.DbTransactionClear();
                SalString msg = "";
                cSessionManager.GetSqlLastError(ref msg);
                WinTranslate.GetTranslate().ShowMessage(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return res;
        }
        public static bool ApprovalRouting_DoRevise(string key_name_, string key_ref_, decimal? step_no_, string approval_note_)
        {
            SalString sSql = "ApprovalRouting_DoRevise(:key_name_,:key_ref_,:step_no_,:approval_note_)";
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("key_name_", key_name_);
            dic.Add("key_ref_", key_ref_);
            dic.Add("step_no_", step_no_);
            dic.Add("approval_note_", approval_note_);
            SalNumber res = cSessionManager.DbMySQLBlock(sSql, dic, System.Data.CommandType.StoredProcedure);
            if (res > 0)
                cSessionManager.DbTransactionEnd();
            else
            {
                cSessionManager.DbTransactionClear();
                SalString msg = "";
                cSessionManager.GetSqlLastError(ref msg);
                WinTranslate.GetTranslate().ShowMessage(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return res;
        }

        public static List<person_group> DisplayPersonGroup()
        {
            List<person_group> ilPersonGroup = person_group.FindAll();
            return ilPersonGroup;
        }

        public static List<person_info> DisplayPersonInfo()
        {
            List<person_info> ilPersonInfo = person_info.FindAll();
            return ilPersonInfo;
        }
    }
}
